package com.xiaoma.tpo.webservice;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiaoma.tpo.domain.ChChapter;
import com.xiaoma.tpo.domain.ChSentence;
import com.xiaoma.tpo.domain.ChWord;
import com.xiaoma.tpo.domain.ChTpo;
import com.xiaoma.tpo.service.ChapterService;

@Controller
@RequestMapping("/service/chapter")
public class ChapterWS {

    private static Logger LOG = LoggerFactory.getLogger(ChapterWS.class);
    
    @Autowired
    private ChapterService chapterService;
    
    @RequestMapping(value = "/tpos", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findAllTpos(){
        List<ChTpo> tpos = chapterService.findAllTpos();
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("tpos", tpos);
        return result;
        
    }
    
    @RequestMapping(value = "/tpos/{tpoId}/chapters", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findChaptersByTpoId(@PathVariable Integer tpoId){
        List<ChChapter> chapters = chapterService.findByTpoId(tpoId);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("chapters", chapters);
        return result;
        
    }
    
    @RequestMapping(value = "/{chapterId}/sentences", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findSentencesByChapterId(@PathVariable Integer chapterId){
        List<ChSentence> chapterSentences = chapterService.findSentencesByChapterId(chapterId);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("chapterSentences", chapterSentences);
        return result;
        
    }
    
    @RequestMapping(value = "/{chapterId}/words", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findWordsByChapterId(@PathVariable Integer chapterId){
        List<ChWord> chapterWords = chapterService.findWordsByChapterId(chapterId);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("chapterWords", chapterWords);
        return result;
        
    }
    
    @RequestMapping(value = "/{chapterId}/wordsAndSentences", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findWordsAndSentencesByChapterId(@PathVariable Integer chapterId){
        List<ChWord> chapterWords = chapterService.findWordsByChapterId(chapterId);
        List<ChSentence> chapterSentences = chapterService.findSentencesByChapterId(chapterId);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("chapterWords", chapterWords);
        result.put("chapterSentences", chapterSentences);
        return result;
        
    }
}
